﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.teacher
{
    public partial class edit_assignment : System.Web.UI.Page
    {
        int assignment_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            assignment_id = Convert.ToInt32(Request.QueryString["id"]);
            if (!Page.IsPostBack)
            {
                populatePage();
            }
        }

        private void populatePage()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var assignemnt = (from a in db.assignments
                              where a.assignment_id == assignment_id
                              select a).SingleOrDefault();

            TextBox1.Text = assignemnt.assignment_title;
            TextBox2.Text = assignemnt.assignment_description;
            DateTime deadline = assignemnt.assignment_deadline;
            TextBox3.Text = deadline.Day + "/" + deadline.Month + "/" + deadline.Year;
            TextBox4.Text = deadline.Hour.ToString();
            TextBox5.Text = deadline.Minute.ToString();
            TextBox6.Text = deadline.Second.ToString();
        }

        private void updateAssignment()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var assignemnt = (from a in db.assignments
                              where a.assignment_id == assignment_id
                              select a).SingleOrDefault();

            assignemnt.assignment_title = TextBox1.Text;
            assignemnt.assignment_description = TextBox2.Text;

            DateTime date = DateTime.Parse(TextBox3.Text);

            int hour = Convert.ToInt32(TextBox4.Text);
            int min = Convert.ToInt32(TextBox5.Text);
            int sec = Convert.ToInt32(TextBox6.Text);

            DateTime deadline = new DateTime(date.Year, date.Month, date.Day, hour, min, sec, 0);
            assignemnt.assignment_deadline = deadline;
            db.SaveChanges();
            goBack();
        }

        private void goBack()
        {
            
            if (Request.QueryString["p"] != null)
            {
                Response.Redirect("viewClass.aspx?id=" + Request.QueryString["p"]);
            }
            else if (Session["prePage"] != null)
            {
                String p = Session["prePage"].ToString();
                Session["prePage"] = null;
                Response.Redirect(p);
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            goBack();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            updateAssignment();
        }
    }
}